package edu.sctu.practice.class02.hujie.online_classes_practice.imooc.Java_XMLRead.JDOM;

import edu.sctu.practice.class02.hujie.online_classes_practice.imooc.Java_XMLRead.SAX.Book;

import org.jdom.Element;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * SAXBuilder 是导入的JDOM.jar包
 * 倒入包的快捷键shift+Ctrl+alt+s
 * 代码上下移 alt+向上键
 * Created by hp on 2016/3/10.
 */
public class JDOMtest {
    private static ArrayList<Book> booksList = new ArrayList<Book>();

    public void parseXML() {
        //进行对books.xml文件的JDOM解析
        //准备工作
        //1.创建一个SAXBuilder的对象
        SAXBuilder saxBuilder = new SAXBuilder();
        InputStream in;
        try {
            //2.创建一个输入流，将xml文件加载到输入流中
            in = new FileInputStream("src\\main\\java\\edu\\sctu\\practice" +
                    "\\class02\\hujie\\online_classes_practice\\imooc\\Java_XMLRead\\books.xml");
            InputStreamReader isr = new InputStreamReader(in, "UTF-8");//解析时乱码问题
            //3.通过saxBuilder的build方法，将输入流加载到saxBuilder中
            Document document = saxBuilder.build(in);
            //4.通过document对象获取xml文件的根节点
            Element rootElement = document.getRootElement();
            //5.获取根节点下的子节点的List集合
            List<Element> bookList = rootElement.getChildren();
            //继续进行解析
            for (Element book : bookList) {
                Book bookEntity = new Book();
                System.out.println("********开始解析第" + (bookList.indexOf(book) + 1) + "本书********");
                //解析book的属性
                List<Attribute> attrList = book.getAttributes();
                //知道节点下属性名称时，获取节点值
//                book.getAttributeValue("id");
                //遍历attribute(真对不清楚book节点下属性的名字及数量)
                for (Attribute attr : attrList) {
                    //获取属性名
                    String attrName = attr.getName();
                    //获取属性值
                    String attrValue = attr.getValue();
                    System.out.println("属性名：" + attrName + "属性值：" + attrValue);
                    if (attrName.equals("id")) {
                        bookEntity.setId(attrValue);
                    }
                }
                //对book节点的子节点的节点名以及节点值的遍历
                List<Element> bookChilds = book.getChildren();
                for (Element child : bookChilds) {
                    System.out.println("节点名：" + child.getName() + "节点值：" + child.getValue());
                    if (child.getName().equals("name")) {
                        bookEntity.setName(child.getValue());

                    } else if (child.getName().equals("year")) {
                        bookEntity.setYear(child.getValue());

                    } else if (child.getName().equals("price")) {
                        bookEntity.setPrice(child.getValue());

                    } else if (child.getName().equals("language")) {
                        bookEntity.setLanguage(child.getValue());

                    } else if (child.getName().equals("author")) {
                        bookEntity.setAuthor(child.getValue());
                    }
                }
                System.out.println("********结束解析第" + (bookList.indexOf(book) + 1) + "本书********");
                booksList.add(bookEntity);
                bookEntity = null;
                System.out.println(booksList.size());
                System.out.println(booksList.get(0).getId());
                System.out.println(booksList.get(0).getName());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void createXML() {
        //1.生成一个根节点
        Element rss=new Element("rss");
        //2.为节点添加属性
        rss.setAttribute("version","2.0");
        //3.生成一个document
        Document document=new Document(rss);

        Element channel=new Element("channel");
        rss.addContent(channel);
        Element title=new Element("title");
        title.setText("宇宙最新新闻");
        channel.addContent(title);
        Format format=Format.getCompactFormat();
        format.setIndent("");//换行
//        format.setEncoding("GBK");
        //4.创建XMLOutputter的对象
        XMLOutputter outputer=new XMLOutputter(format);

        //5.利用output将document对象转换成xml文档
        try {
            outputer.output(document,new FileOutputStream(new File("src\\main\\java\\edu\\sctu\\practice\\class02\\hujie\\online_classes_practice\\imooc\\Java_XMLRead\\JDOM\\rssnews.xml")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        JDOMtest test = new JDOMtest();
        test.createXML();
    }
}
